package com.archgeek.java.conc.future;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author pizhihui
 * @date 2024-03-19
 */
public class FutureTaskDemo {


    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {

        FutureTask<Integer> task = new FutureTask<>(() -> {

            TimeUnit.SECONDS.sleep(10);

            return 100;
        });

        new Thread(task, "t1").start();

        // 一
        // System.out.println(task.get());

        // 二
        // System.out.println(task.get(4, TimeUnit.SECONDS));

        // 三
        while (true) {
            if (task.isDone()) {
                System.out.println(task.get());
                break;
            } else {
                System.out.println("轮训等待...");
            }
        }

        // 四

        System.out.println("主程序中....");

    }

}
